<template>
  <div class="user-module">
    <div class="user-header">
      <font-awesome-icon :icon="['fas', 'comment']" /> 用户列表
    </div>
    <!-- 新增搜索输入框 -->
    <input
        v-model="searchQuery"
        type="text"
        class="user-search"
        placeholder="请输入用户名称"
    />
    <div class="user-list">
      <!-- 循环渲染用户列表项（使用users数组，后端已过滤） -->
      <div class="user-item" v-for="user in users" :key="user.id">
        <!-- 用户头像区域 -->
        <div class="user-avatar">
          <img
              :src="user.avatar || 'http://110.40.229.202//api/pms/images/18952334-4f0c-442a-ae7d-911949fd1895.jpg'"
              alt="用户头像"
          />
        </div>
        <!-- 用户信息区域 -->
        <div class="user-info">
          <div class="user-nickname">{{ user.nickname }}</div>
          <div class="user-status">
            <font-awesome-icon
                :icon="['fas', 'circle']"
                :style="{ color: user.status === 1 ? 'green' : 'red' }"
            />
          </div>
        </div>
        <!-- 用户操作区域 -->
        <div class="user-action">
          <font-awesome-icon
              :icon="['fas', 'phone-slash']"
              style="color: red; cursor: pointer;"
              @click="hangUpSingleChat(calling.callId)"
              v-if="calling != null && calling.type === callType.single && (calling.caller.id === user.id || calling.callee.id === user.id)"
          />
          <font-awesome-icon
              :icon="['fas', 'phone']"
              style="color: #07c160; cursor: pointer;"
              @click="call(user)"
              v-else
          />
        </div>
      </div>
    </div>
    <div class="user-pagination">
      <el-pagination
          style="float: right;"
          background
          :small="true"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="size"
          :layout="layout"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {list} from "@/api/http/user";

export default {
  name: "User",
  inject: ['callCommand' , 'callType'],
  props:{
    callSingleChat: {
      type: Function
    },
    calling: {
      type: Object,
    },
    hangUpSingleChat: {
      type: Function
    },
    confirmHangup:{
      type: Function
    }
  },
  data() {
    return {
      users: [],
      searchQuery: "",
      currentPage: 1,
      size: 10,
      total: 0,
      layout: "prev , pager , next , jumper",
    };
  },
  watch: {
    // 搜索词变化时重置分页并重新加载
    searchQuery() {
      this.loadUser();
    }
  },
  methods: {
    // 初始化加载第一页数据
    async loadUser() {
      const result = await list({
        current: this.currentPage,
        size: this.size,
        nickname: this.searchQuery
      });
      if (result && result.code === 0){
        this.users = result.data.records;
        this.total = result.data.total;
      }
    },
    handleSizeChange(pageSize) {
      this.size = pageSize
    },
    async handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      await this.loadUser();
    },
    async call(user){
      // 确认是否关闭当前通话
      if (!(await this.confirmHangup())){
        return
      }
      this.callSingleChat(user)
    }
  },
  async mounted() {
    await this.loadUser(); // 初始加载第一页
  }
};
</script>

<style scoped>


.user-module {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 1.2rem 0.6rem;
  gap: 0.6rem;
  min-height: 0;
  overflow: hidden;
  box-sizing: border-box;
}

.user-header {
  display: flex;
  align-items: center;
  gap: 0.5vw;
  font-size: 1.1rem;
  font-weight: bold;
  color: #fff;
  margin-bottom: 0.5rem;
}

/* 新增搜索输入框样式 */
.user-search {
  /* 确保包含内边距和边框的总宽度不超出容器 */
  box-sizing: border-box;
  width: 100%;
  padding: 0.35rem 0.6rem;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 0.25rem;
  font-size: 0.9rem;
  background-color: #333;
  color: #fff;
  margin-bottom: 0.8rem;
}


.user-list {
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
  flex: 1; /* 2. 占据父容器剩余空间 */
  overflow-y: auto; /* 3. 内容溢出时显示内部滚动条 */
  min-height: 200px;
  /* 添加内边距补偿，避免内容紧贴滚动条 */
  padding-right: 8px;
  max-height: calc(100% - 120px); /* 减去头部和搜索框高度 */
}

.user-item {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  overflow: hidden;
}

.user-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.user-info {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
}

.user-nickname {
  font-size: 1rem;
  font-weight: bold;
  color: #fff;
}

.user-status {
  font-size: 0.8rem;
}

.user-action {
  font-size: 1.2rem;
}

input{
  outline: none;
}
</style>